

export interface RTPPacket {
    sequenceNumber: number;
    payloadType: number;
    timestamp: number;
    marker: number;
    payload: Buffer;
    ssrc: number;
}

/**
 * 
 * @param type 解析RTP包头
 * @param buffer 
 */
export function parseRTPPacket(buffer: Buffer): RTPPacket {
    // 总共12个字节 + hasExtensions 多4个字节 + num_csrc_identifiers*4
    const hasExtensions = (buffer[0] >> 4) & 0x01;
    const num_csrc_identifiers = (buffer[0] & 0x0F);
    const marker = (buffer[1]) >>> 7;
    const payloadType = (buffer[1] & 0x7F);
    const sequenceNumber = buffer.readUInt16BE(2);
    const timestamp = buffer.readUInt32BE(4);
    const ssrc = buffer.readUInt32BE(8);
    const payload = buffer.slice((num_csrc_identifiers * 4) + (hasExtensions ? 16 : 12));

    return {
        sequenceNumber,
        payloadType,
        timestamp,
        marker,
        payload,
        ssrc,
    };
}